﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;

namespace YouHP.Pages
{
    public class TreatmentStat : YouHP.Pages.BasePage
    {
        protected TreatmentInfo Info = null;
        protected int tid = -1;

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            if (IsPost == false) HandleGet();
        }

        protected void HandleGet()
        {
            int type = RequestTools.GetInt("type", 0);
            
            tid = RequestTools.GetQueryInt("tid", 0);
            if (tid <= 0)
            {
                return;
            }

            Info = TreatmentBLL.Instance.GetTreatmentInfo(tid);
            if (Info.Id < 0)
            {
                return;
            }

            switch(type)
            {
            case 1:
                MakeTaketimeGraph();
                break;

            case 2: 
                MakeDosageGraph();
                break;

            case 3:
                MakeStartReasonGraph();
                break;

            case 4:
                MakeStopReasonGraph();
                break;

            default:
                break;
            }
        }

        private void MakeTaketimeGraph()
        {
            List<TreatmentStatTaketime> lst = TreatmentBLL.Instance.GetTreatmentStatTaketime(tid);
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("&Histogram=1&\r\n");
            int maxcnt = 0;
            int i = 0;
            foreach (TreatmentStatTaketime info in lst)
            {
                sb.AppendFormat("&Data_{0}={1};{2}&\r\n", i, info.Taketime, info.PatientCount);
                i++;
                if (maxcnt < info.PatientCount) maxcnt = info.PatientCount;
            }
            sb.AppendFormat("&Var={0}&;\r\n", lst.Count);
            sb.AppendFormat("&MaxValue={0}&\r\n", maxcnt);

            ResponseXML(sb);
        }

        private void MakeDosageGraph()
        {
            int tid = RequestTools.GetQueryInt("tid", 0);
            if (tid <= 0) return;
            List<TreatmentStatDosage> lst = TreatmentBLL.Instance.GetTreatmentStatDosage(tid);
            
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("&Bar=1&\r\n");

            int i = 0;
            string linkStr = "";
            foreach (TreatmentStatDosage info in lst)
            {
                if (info.PatientCount > 0)
                {
                    linkStr = ""/*GetUrl("/PatientList.aspx?dszid=" + info.)*/;
                }
                else
                {
                    linkStr = "";
                }

                sb.AppendFormat("&Data_{0}={1} {2}/{3};{4};{5}&\r\n", i, info.DataValue, Info.MeasurementUnits, Info.Frequency, linkStr, info.PatientCount);
                i++;
            }
            sb.AppendFormat("&Var=治疗剂量统计;{0}&;\r\n", lst.Count);

            ResponseXML(sb);
        }

        private void MakeStartReasonGraph()
        {
            int tid = RequestTools.GetQueryInt("tid", 0);
            if (tid <= 0) return;
            List<TreatmentStartReason> lst = TreatmentBLL.Instance.GetTreatmentStartReason(tid);
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("&Pie=1&\r\n");
            int maxcnt = 0;
            int i = 0;
            foreach (TreatmentStartReason info in lst)
            {
                sb.AppendFormat("&Data_{0}={1};{2};{3}&\r\n", i, info.StartReason, "", info.PatientCount);
                i++;
                if (maxcnt < info.PatientCount) maxcnt = info.PatientCount;
            }
            sb.AppendFormat("&Var={0}&;\r\n", lst.Count);

            ResponseXML(sb);
        }

        private void MakeStopReasonGraph()
        {
            int tid = RequestTools.GetQueryInt("tid", 0);
            if (tid <= 0) return;
            List<TreatmentStopReason> lst = TreatmentBLL.Instance.GetTreatmentStopReason(tid);
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("&Pie=1&\r\n");
            int maxcnt = 0;
            int i = 0;
            foreach (TreatmentStopReason info in lst)
            {
                sb.AppendFormat("&Data_{0}={1};{2};{3}&\r\n", i, info.StopReason, "", info.PatientCount);
                i++;
                if (maxcnt < info.PatientCount) maxcnt = info.PatientCount;
            }
            sb.AppendFormat("&Var={0}&;\r\n", lst.Count);

            ResponseXML(sb);

        }

        private void ResponseXML(StringBuilder sb)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ContentType = "Text/html";
            System.Web.HttpContext.Current.Response.Expires = 0;

            System.Web.HttpContext.Current.Response.Write(sb.ToString());
            System.Web.HttpContext.Current.Response.End();
        }
    }
}